#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
扩展数据库模型
包含积分系统、成就系统、用户行为统计和社区功能的数据模型
"""

from flask_sqlalchemy import SQLAlchemy
import datetime

# 这些模型将被添加到主应用的数据库中

# 用户积分记录模型
class UserPoints(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    points = db.Column(db.Integer, default=0)  # 当前积分
    total_earned = db.Column(db.Integer, default=0)  # 累计获得积分
    level = db.Column(db.Integer, default=1)  # 用户等级
    level_name = db.Column(db.String(50), default='环保新手')  # 等级名称
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
    
    user = db.relationship('User', backref='points_record')

# 积分变动记录模型
class PointsHistory(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    points_change = db.Column(db.Integer, nullable=False)  # 积分变动（正数为获得，负数为消费）
    reason = db.Column(db.String(100), nullable=False)  # 积分变动原因
    description = db.Column(db.String(255))  # 详细描述
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    
    user = db.relationship('User', backref='points_history')

# 成就系统模型
class Achievement(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)  # 成就名称
    description = db.Column(db.String(255), nullable=False)  # 成就描述
    icon_url = db.Column(db.String(255))  # 成就图标URL
    condition_type = db.Column(db.String(50), nullable=False)  # 条件类型：prediction_count, consecutive_days, accuracy_rate等
    condition_value = db.Column(db.Integer, nullable=False)  # 条件数值
    points_reward = db.Column(db.Integer, default=0)  # 完成成就获得的积分
    is_active = db.Column(db.Boolean, default=True)  # 是否启用
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)

# 用户成就记录模型
class UserAchievement(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    achievement_id = db.Column(db.Integer, db.ForeignKey('achievement.id'), nullable=False)
    achieved_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    
    user = db.relationship('User', backref='achievements')
    achievement = db.relationship('Achievement', backref='users_achieved')

# 用户行为统计模型
class UserStats(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    total_predictions = db.Column(db.Integer, default=0)  # 总识别次数
    correct_predictions = db.Column(db.Integer, default=0)  # 正确识别次数
    accuracy_rate = db.Column(db.Float, default=0.0)  # 准确率
    consecutive_days = db.Column(db.Integer, default=0)  # 连续使用天数
    last_active_date = db.Column(db.Date)  # 最后活跃日期
    favorite_category = db.Column(db.String(50))  # 最常识别的垃圾类型
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
    
    user = db.relationship('User', backref='stats')

# 系统统计模型（按日期统计）
class SystemStats(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    date = db.Column(db.Date, nullable=False, unique=True)  # 统计日期
    active_users = db.Column(db.Integer, default=0)  # 活跃用户数
    total_predictions = db.Column(db.Integer, default=0)  # 总识别次数
    new_users = db.Column(db.Integer, default=0)  # 新注册用户数
    popular_category = db.Column(db.String(50))  # 热门垃圾类型
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)

# 论坛帖子模型
class ForumPost(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(200), nullable=False)
    content = db.Column(db.Text, nullable=False)
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    category = db.Column(db.String(50), default='general')  # 帖子分类：general, tips, qa
    is_pinned = db.Column(db.Boolean, default=False)  # 是否置顶
    view_count = db.Column(db.Integer, default=0)  # 浏览次数
    like_count = db.Column(db.Integer, default=0)  # 点赞数
    reply_count = db.Column(db.Integer, default=0)  # 回复数
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow)
    
    author = db.relationship('User', backref='forum_posts')

# 论坛回复模型
class ForumReply(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    post_id = db.Column(db.Integer, db.ForeignKey('forum_post.id'), nullable=False)
    author_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    content = db.Column(db.Text, nullable=False)
    like_count = db.Column(db.Integer, default=0)  # 点赞数
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    
    post = db.relationship('ForumPost', backref='replies')
    author = db.relationship('User', backref='forum_replies')

# 用户点赞记录模型
class UserLike(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    target_type = db.Column(db.String(20), nullable=False)  # 'post' 或 'reply'
    target_id = db.Column(db.Integer, nullable=False)  # 帖子或回复的ID
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    
    user = db.relationship('User', backref='likes')

# 用户签到记录模型
class UserCheckin(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    checkin_date = db.Column(db.Date, nullable=False)
    points_earned = db.Column(db.Integer, default=5)  # 签到获得的积分
    consecutive_days = db.Column(db.Integer, default=1)  # 连续签到天数
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    
    user = db.relationship('User', backref='checkins')
    
    # 确保每个用户每天只能签到一次
    __table_args__ = (db.UniqueConstraint('user_id', 'checkin_date', name='unique_user_checkin'),)

# 积分商城商品模型
class PointsShopItem(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100), nullable=False)  # 商品名称
    description = db.Column(db.String(255))  # 商品描述
    points_cost = db.Column(db.Integer, nullable=False)  # 所需积分
    item_type = db.Column(db.String(50), default='virtual')  # 商品类型：virtual, tip, badge
    content = db.Column(db.Text)  # 商品内容（如环保小贴士的内容）
    icon_url = db.Column(db.String(255))  # 商品图标
    is_available = db.Column(db.Boolean, default=True)  # 是否可兑换
    stock = db.Column(db.Integer, default=-1)  # 库存（-1表示无限）
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)

# 用户兑换记录模型
class UserPurchase(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)
    item_id = db.Column(db.Integer, db.ForeignKey('points_shop_item.id'), nullable=False)
    points_spent = db.Column(db.Integer, nullable=False)  # 花费的积分
    status = db.Column(db.String(20), default='completed')  # 兑换状态
    created_at = db.Column(db.DateTime, default=datetime.datetime.utcnow)
    
    user = db.relationship('User', backref='purchases')
    item = db.relationship('PointsShopItem', backref='purchases')